import 'package:star_author/utils/common_util/log_util.dart';
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
import 'package:flutter/material.dart';

class ScrollWidget extends StatefulWidget {
  final Widget child;
  final Future<void> Function() onRefresh;
  final Function()? onStart;
  final Function()? onEnd;
  final bool refresh;

  ScrollWidget({
    Key? key,
    required this.child,
    required this.onRefresh,
    this.onStart,
    this.onEnd,
    this.refresh = false,
  }) : super(key: key);

  _ScrollWidgetState createState() => _ScrollWidgetState();
}

class _ScrollWidgetState extends State<ScrollWidget>
    with TickerProviderStateMixin {
  IndicatorState? _prevState;
  late AnimationController _circleController;
  double _offsetToArmed = 80;

  @override
  void initState() {
    if (widget.refresh) {
      _initCircleAnimationController();
    }
    super.initState();
  }

  @override
  void dispose() {
    _circleController.dispose();
    super.dispose();
  }

  void _initCircleAnimationController() {
    _circleController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 1000),
    );
  }

  @override
  Widget build(BuildContext ctx) {
    return widget.refresh
        ? CustomRefreshIndicator(
            offsetToArmed: _offsetToArmed,
            child: widget.child,
            onRefresh: widget.onRefresh,
            builder: (
              BuildContext ctx,
              Widget child,
              IndicatorController controller,
            ) {
              return AnimatedBuilder(
                animation: controller,
                child: child,
                builder: (BuildContext _ctx, Widget? _child) {
                  final currentState = controller.state;
                  if (currentState == IndicatorState.dragging &&
                      widget.onStart != null) {
                    widget.onStart!();
                  } else if (currentState == IndicatorState.idle &&
                      widget.onEnd != null) {
                    widget.onEnd!();
                  }
                  if (_prevState != null) {
                    if (_prevState! == IndicatorState.armed &&
                        currentState == IndicatorState.loading) {
                      LogUtil.v('开始刷新');
                      _circleController.repeat();
                    } else if (_prevState == IndicatorState.loading &&
                        currentState == IndicatorState.hiding) {
                      LogUtil.v('刷新完成');
                      _circleController.stop();
                    } else if (_prevState == IndicatorState.hiding &&
                        currentState != _prevState) {
                      LogUtil.v('隐藏');
                      _circleController.stop();
                    }
                  }
                  _prevState = currentState;
                  return Column(
                    children: [
                      Container(
                        height: _offsetToArmed * controller.value,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            AnimatedBuilder(
                              animation: _circleController,
                              builder: (__ctx, __child) {
                                return Transform.rotate(
                                  angle: currentState == IndicatorState.loading
                                      ? 25 * _circleController.value
                                      : -10 * controller.value,
                                  child: Image.asset(
                                    'images/lt_common/logo_circle.png',
                                    height:
                                        _offsetToArmed * controller.value * 0.5,
                                  ),
                                );
                              },
                            )
                          ],
                        ),
                      ),
                      Expanded(child: child)
                    ],
                  );
                },
              );
            },
          )
        : widget.child;
  }
}
